﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Embedded.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.Common;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper;
    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for an SQL task that uses an embedded resource
    /// </summary>
    /// <typeparam name="TTaskResultClass">
    /// The type of the task result class
    /// </typeparam>
    /// <typeparam name="TDatabaseHelperClass">
    /// The type of the database helper class
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The type of the SQL parameter class
    /// </typeparam>
    public abstract class Embedded<TTaskResultClass, TDatabaseHelperClass, TParameterClass> :
        Base<TTaskResultClass, TDatabaseHelperClass, TParameterClass>
        where TTaskResultClass : Base, new()
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TParameterClass : DbParameter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Embedded{TTaskResultClass,TDatabaseHelperClass,TParameterClass}"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// Name of the task
        /// </param>
        /// <param name="connectionString">
        /// Database connection string
        /// </param>
        /// <param name="assembly">
        /// The assembly containing the resource
        /// </param>
        /// <param name="resourceNamespace">
        /// Namespace where the resource file resides. Usually the application name
        /// </param>
        /// <param name="resourceFileName">
        /// The filename of the embedded resource
        /// </param>
        /// <param name="assumeSqlSafe">
        /// Whether to skip the SQL injection safety check.  Use only if you know the code you are passing in is safe and contains a quoted string that is preventing it passing the saftey check
        /// </param>
        protected Embedded(
            string taskName, 
            string connectionString, 
            Assembly assembly, 
            string resourceNamespace, 
            string resourceFileName, 
            bool assumeSqlSafe)
            : base(
                taskName,
                connectionString)
        {
            this.Assembly = assembly;
            this.ResourceNamespace = resourceNamespace;
            this.ResourceFileName = resourceFileName;
            this.AssumeSqlSafe = assumeSqlSafe;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the assembly containing the resource
        /// </summary>
        protected Assembly Assembly { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether to assume the SQL is safe and don't do the SQL injection safety check
        /// </summary>
        protected bool AssumeSqlSafe { get; set; }

        /// <summary>
        /// Gets or sets the filename of the embedded resource
        /// </summary>
        protected string ResourceFileName { get; set; }

        /// <summary>
        /// Gets or sets the namespace where the resource file resides. Usually the application name
        /// </summary>
        protected string ResourceNamespace { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the SQL to execute
        /// </summary>
        /// <returns>
        /// the SQL to execute
        /// </returns>
        protected override string GetSql()
        {
            return ReflectionExtensions.LoadStringFromResource(
                this.Assembly, this.ResourceNamespace, this.ResourceFileName);
        }

        #endregion
    }
}